package com.yair_srm.integration.sap.impl;

import java.util.ArrayList;

import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoStructure;
import com.sap.conn.jco.JCoTable;
import com.yair_srm.integration.IPOSAO;
import com.yair_srm.integration.sap.IPO_SAP;
import com.yair_srm.integration.sap.ISAP;
import com.yair_srm.model.bean.PurchaseOrder;
import com.yair_srm.model.bean.PurchaseOrderItem;

public class PO_SAP implements IPO_SAP,IPOSAO{
    private SAP_AS_POOL sapconnector = SAP_AS_POOL.getInstance();
    private String functionname;
    private JCoFunction function;
    
    public PO_SAP(){
    	sapconnector = SAP_AS_POOL.getInstance();
    	functionname = "ZSRM_GET_PO";
    	try {
			function = sapconnector.fetchfunction(functionname);
		} catch (JCoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    public PurchaseOrder getPoHeader(String vendor,String ponumber){
    	
    	PurchaseOrder po = new PurchaseOrder();
    	ArrayList<PurchaseOrderItem> items = new ArrayList<PurchaseOrderItem>();
    	
    	JCoParameterList import_p = function.getImportParameterList();    	
 	
    	import_p.setValue("PONUMBER",ponumber);
    	import_p.setValue("VENDOR", vendor);
    	try {
			function.execute(SAP_AS_POOL.getDestination());
			JCoStructure poheader = function.getExportParameterList().getStructure("HEADER");
			
			po.setOrder_date(poheader.getString("CREATION_DATE"));
			po.setOrder_id(poheader.getString("PONUMBER"));			
			po.setOrder_type(poheader.getString("ORDER_TYPE"));
			po.setCompany(poheader.getString("COMPANY"));
			po.setOrganization(poheader.getString("ORG"));			
			po.setGroup(poheader.getString("GRP"));
			po.setTel(poheader.getString("TEL"));
			po.setFax(poheader.getString("FAX"));
			
			JCoTable poitem = function.getTableParameterList().getTable("ITEMS");
			
			do{
				PurchaseOrderItem item = new PurchaseOrderItem();
				item.setItem_no(poitem.getString("ITEM_NO"));
				item.setMaterial_text(poitem.getString("MATERIAL_TEXT"));
				item.setQuantity(poitem.getString("QUANTITY"));
//				item.setText(poitem.getString(""));
				item.setMaterial_no(poitem.getString("MATERIAL_NO"));
				item.setUnit(poitem.getString("UNIT"));
				items.add(item);
			}while(poitem.nextRow());
			
			po.setItems(items);
						
		} catch (JCoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return po;
    }
    
    public static void main(String[] args){
    	PO_SAP posap = new PO_SAP();
    	PurchaseOrder po = posap.getPoHeader("5550","3000000004");
    	ArrayList<PurchaseOrderItem> items = po.getItems();
    	for(int i = 0;i<items.size();i++)
    		System.out.println(items.get(i).getMaterial_text());
        System.out.println(po.getCompany());
        System.out.println(po.getGroup());
        
    }

}
